// 挑战_周年庆爬塔
threads.start(function () {
  sleep(1000); //反悔时间
  var beginBtn;
  if (
    (beginBtn = classNameContains("Button")
      .textContains("立即开始")
      .findOne(2000))
  ) {
    beginBtn.click();
  }
});

if (!requestScreenCapture()) {
  console.log(msg);
  exit();
}

var text = "每日挑战";
var text2 = "点击屏幕继续";
let frequency = 0; // 执行次数

sleep(1000);
// click(1634, 1056);

const formatting = (data) => JSON.parse(JSON.stringify(data));

const init = () => {
  const img = captureScreen();

  const result = gmlkit.ocr(img, "zh");
  sleep(10);
  img.recycle(); //释放内存

  let res = formatting(result.children).find(
    (obj) => obj.text.indexOf(text) != -1
  );
  if (res === undefined) {
    let res2 = formatting(result.children).find(
      (obj) => obj.text.indexOf(text2) != -1
    );
    sleep(10);
    res2 && fnclick(res2.bounds);
    click(10, 10);
  } else {
    fnclick(res.bounds);
    frequency++;
    console.log(`执行次数: ${frequency}`);
    console.log(`消耗体力: ${frequency * 6}`);
  }
};

function fnclick(item) {
  click(item.left, item.bottom);
}
setInterval(init, 4000);
